package com.mnt.washio.activities;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.mnt.washio.R;
import com.mnt.washio.adapters.AddressListAdapter;
import com.mnt.washio.adapters.AddressListAdapter.OnItemAdressClickListener;
import com.mnt.washio.objects.Address;
import com.mnt.washio.utils.SessionManager;
import com.mnt.washio.utils.network.WebServiceCommunicatorListener;
import com.mnt.washio.utils.network.ws.BaseWSControl;
import com.mnt.washio.utils.network.ws.BaseWSControl.WebServiceFlag;
import com.mnt.washio.utils.network.ws.GetAddressListWSControl;
import com.mnt.washio.views.CustomFontButton;
import com.parse.ParseUser;

public class AddressActivity extends BaseActivity implements OnClickListener,
		OnItemAdressClickListener, WebServiceCommunicatorListener {

	private ListView mAddressList;
	private AddressListAdapter mAdapter;
	private ArrayList<Address> mAddresses;
	private CustomFontButton mBtnAddAddress;
	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address);

		initView();
		initProperties();
		addListeners();

	}

	@Override
	protected void initView() {
		super.initView();
		mBtnAddAddress = (CustomFontButton) findViewById(R.id.btn_add_address);
		mAddressList = (ListView) findViewById(R.id.list_address);
		mAddresses = new ArrayList<Address>();
		mAdapter = new AddressListAdapter(this, mAddresses);

	}

	private void initProperties() {
		pd = new ProgressDialog(this);
		pd.setCancelable(false);
		mAddressList.setAdapter(mAdapter);
		loadData();
	}

	public void loadData() {
		String userObjectId = "";
		userObjectId = ParseUser.getCurrentUser().getObjectId();

		new GetAddressListWSControl(this, this, userObjectId).execute();
	}

	private void addListeners() {
		mBtnAddAddress.setOnClickListener(this);
		mAdapter.setOnItemAdressClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btn_add_address) {
			Intent i = new Intent(this, RegisterStep2Activity.class);
			startActivityForResult(i, 100);
		}
	}

	@Override
	public void onConnectionOpen(BaseWSControl wsControl, WebServiceFlag flag) {
		// TODO Auto-generated method stub
		pd.setMessage("Loading...");
		pd.show();
	}

	@Override
	public void onConnectionError(BaseWSControl wsControl, WebServiceFlag flag,
			String error) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "Error : " + error, Toast.LENGTH_SHORT).show();
		pd.dismiss();
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onConnectionDone(BaseWSControl wsControl, WebServiceFlag flag,
			Object result) {
		// TODO Auto-generated method stub
		if (flag == WebServiceFlag.GET_ADDRESS) {
			mAddresses.clear();
			mAddresses.addAll((ArrayList<Address>) result);
			mAdapter.notifyDataSetChanged();

			SessionManager.getSessionManager(this).setOurArrayAddress(
					(ArrayList<Address>) result);
		}
		pd.dismiss();
	}

	@Override
	public void onUpdateData(BaseWSControl wsControl, WebServiceFlag flag,
			Object data) {
		// TODO Auto-generated method stub
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 100 && resultCode == RESULT_OK) {
			loadData();
		}
	}

	@Override
	public void onItemClick(Address add) {
		Intent i = new Intent(this, RegisterStep2Activity.class);
		i.putExtra("ADDRESS", add);
		startActivityForResult(i, 100);
	}

}
